﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Drawing.Imaging;

namespace ImageProcessing
{
    class MorphologyOperations
    {

        public static ImageStructure[,] Dilation(ImageStructure[,] image, byte[,] mask, Point center)
        {
            int maskHeight = mask.GetLength(0);
            int maskWidth = mask.GetLength(1);
            int height = image.GetLength(0);
            int width = image.GetLength(1);
            int maskHeightMinusCenter = maskHeight - center.Y - 1;
            int maskWidthMinusCenter = maskWidth - center.X - 1;
            int maskHeightMinusOne = maskHeight - 1;
            int maskWidthMinusOne = maskWidth - 1;
            ImageStructure[,] newImage = BinaryThreshold(image);
            bool found = false;

            mask = ReflectMask(mask, maskHeight, maskWidth);

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    for (int m = 0; m < maskHeight; m++)
                    {
                        for (int l = 0; l < maskWidth; l++)
                        {
                            if (i + maskHeightMinusCenter - m < 0 || i + maskHeightMinusCenter - m >= height || j + maskWidthMinusCenter - l < 0 || j + maskWidthMinusCenter - l >= width)
                            {
                                continue;
                            }
                            if (image[i + maskHeightMinusCenter - m, j + maskWidthMinusCenter - l].R != 0 && mask[maskHeightMinusOne - m, maskWidthMinusOne - l] != 0)
                            {
                                found = true;
                                newImage[i, j].R = newImage[i, j].G = newImage[i, j].B = 255;
                                break;
                            }
                        }
                        if (found)
                        {
                            break;
                        }
                    }
                    if (!found)
                    {
                        newImage[i, j].R = newImage[i, j].G = newImage[i, j].B = 0;
                    }
                    found = false;
                }
            }
            return newImage;
        }

        public static ImageStructure[,] Erosion(ImageStructure[,] image, byte[,] mask, Point center)
        {
            int maskHeight = mask.GetLength(0);
            int maskWidth = mask.GetLength(1);
            int height = image.GetLength(0);
            int width = image.GetLength(1);
            int maskHeightMinusCenter = maskHeight - center.Y - 1;
            int maskWidthMinusCenter = maskWidth - center.X - 1;
            int maskHeightMinusOne = maskHeight - 1;
            int maskWidthMinusOne = maskWidth - 1;
            ImageStructure[,] newImage = BinaryThreshold(image);
            bool found = false;

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    for (int m = 0; m < maskHeight; m++)
                    {
                        for (int l = 0; l < maskWidth; l++)
                        {
                            if (i + maskHeightMinusCenter - m < 0 || i + maskHeightMinusCenter - m >= height || j + maskWidthMinusCenter - l < 0 || j + maskWidthMinusCenter - l >= width)
                            {
                                continue;
                            }
                            if (image[i + maskHeightMinusCenter - m, j + maskWidthMinusCenter - l].R == 0 && mask[maskHeightMinusOne - m, maskWidthMinusOne - l] != 0)
                            {
                                found = true;
                                newImage[i, j].R = newImage[i, j].G = newImage[i, j].B = 0;
                                break;
                            }
                        }
                        if (found)
                        {
                            break;
                        }
                    }
                    if (!found)
                    {
                        newImage[i, j].R = newImage[i, j].G = newImage[i, j].B = 255;
                    }
                    found = false;
                }
            }
            return newImage;
        }

        public static byte[,] ReflectMask(byte[,] mask, int height, int width)
        {
            int heightMinusOne = height - 1;
            int widthMinusOne = width - 1;
            byte[,] newMask = new byte[height, width];

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    newMask[i, j] = mask[heightMinusOne - i, widthMinusOne - j];
                }
            }
            return newMask;
        }

        public static ImageStructure[,] BinaryThreshold(ImageStructure[,] image)
        {
            int height = image.GetLength(0);
            int width = image.GetLength(1);

            ImageStructure[,] newimage = Operations.GreyScale(image);

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (newimage[i, j].R < 128)
                    {
                        newimage[i, j].R = newimage[i, j].G = newimage[i, j].B = 0;
                    }
                    else
                    {
                        newimage[i, j].R = newimage[i, j].G = newimage[i, j].B = 255;
                    }
                }
            }
            return newimage;
        }

        public static Bitmap DilationFast(Bitmap img, byte[,] mask, Point center)
        {
            int imgHeight = img.Height;
            int imgWidth = img.Width;
            int mskHeight = mask.GetLength(0);
            int mskWidth = mask.GetLength(1);


            Bitmap bmpR = new Bitmap(imgWidth, imgHeight, PixelFormat.Format24bppRgb);

            unsafe
            {
                BitmapData bmd, bmdR;
                bmd = img.LockBits(new Rectangle(0, 0, imgWidth, imgHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                bmdR = bmpR.LockBits(new Rectangle(0, 0, imgWidth, imgHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                int extraBytes = bmd.Stride - imgWidth * 3;
                PHFastCpp.PHDilation((int)bmd.Scan0, (int)bmdR.Scan0, imgWidth, imgHeight, extraBytes, mask, mskHeight, mskWidth, (int)center.X, (int)center.Y);

                img.UnlockBits(bmd);
                bmpR.UnlockBits(bmdR);

                return bmpR;
            }
        }

        public static Bitmap ErosionFast(Bitmap img, byte[,] mask, Point center)
        {
            int imgHeight = img.Height;
            int imgWidth = img.Width;
            int mskHeight = mask.GetLength(0);
            int mskWidth = mask.GetLength(1);


            Bitmap bmpR = new Bitmap(imgWidth, imgHeight, PixelFormat.Format24bppRgb);

            unsafe
            {
                BitmapData bmd, bmdR;
                bmd = img.LockBits(new Rectangle(0, 0, imgWidth, imgHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                bmdR = bmpR.LockBits(new Rectangle(0, 0, imgWidth, imgHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                int extraBytes = bmd.Stride - imgWidth * 3;
                PHFastCpp.PHDErosion((int)bmd.Scan0, (int)bmdR.Scan0, imgWidth, imgHeight, extraBytes, mask, mskHeight, mskWidth, (int)center.X, (int)center.Y);

                img.UnlockBits(bmd);
                bmpR.UnlockBits(bmdR);

                return bmpR;
            }
        }

        public static Bitmap DilationFastSharp(Bitmap img, byte[] pMask, Point center)
        {
            int height = img.Height; int width = img.Width;
            int mskHeight = 3; int mskWidth = 3;
            Bitmap bmpR = new Bitmap(width, height, PixelFormat.Format24bppRgb);

            BitmapData bmd, bmdR;

            bmd = img.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            bmdR = bmpR.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int extraBytes = bmd.Stride - width * 3;
            unsafe
            {
                int i, j, m, l, pixelIndex, mskIndex, newPixelIndex;
                byte* pOriginal = (byte*)bmd.Scan0;
                byte* pNew = (byte*)bmdR.Scan0;
                // byte* pMask = (byte*)mask;
                int maskHeightMinusCenter = mskHeight - center.Y - 1;
                int maskWidthMinusCenter = mskWidth - center.X - 1;
                int maskHeightMinusOne = mskHeight - 1;
                int maskWidthMinusOne = mskWidth - 1;
                int mskSizeMinus1 = (mskHeight * mskWidth) - 1;
                int imgSize = height * width;
                bool found = false;
                {
                    for (i = 0; i < height; i++)
                    {
                        for (j = 0; j < width; j++)
                        {
                            pixelIndex = (i * width * 3) + (i * extraBytes) + (j * 3);
                            for (m = 0; m < mskHeight; m++)
                            {
                                for (l = 0; l < mskWidth; l++)
                                {
                                    newPixelIndex = ((i + maskHeightMinusCenter - m) * width * 3) + ((i + maskHeightMinusCenter - m) * extraBytes) + ((j + maskWidthMinusCenter - l) * 3);
                                    mskIndex = mskSizeMinus1 - ((m * mskHeight) + (l));

                                    if (newPixelIndex < 0 || newPixelIndex >= imgSize)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        if (pNew[newPixelIndex] == 0 && pMask[mskIndex] != 0)
                                        {
                                            found = true;
                                            pNew[pixelIndex] = pNew[pixelIndex + 1] = pNew[pixelIndex + 2] = 0;
                                            break;
                                        }
                                    }
                                }
                                if (found)
                                {
                                    found = false;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                pNew[pixelIndex] = pNew[pixelIndex + 1] = pNew[pixelIndex + 2] = 255;
                            }
                        }
                    }
                    img.UnlockBits(bmd);
                    bmpR.UnlockBits(bmdR);

                    return bmpR;
                }
            }
        }

        public static Bitmap ThresholdSharp(Bitmap img, int thresholdValue)
        {
            int imgHeight = img.Height;
            int imgWidth = img.Width;


            Bitmap bmpR = new Bitmap(imgWidth, imgHeight, PixelFormat.Format24bppRgb);

            unsafe
            {
                BitmapData bmd, bmdR;
                bmd = img.LockBits(new Rectangle(0, 0, imgWidth, imgHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                bmdR = bmpR.LockBits(new Rectangle(0, 0, imgWidth, imgHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                int extraBytes = bmd.Stride - imgWidth * 3;
                PHFastCpp.PHBinaryThreshold((int)bmd.Scan0, (int)bmdR.Scan0, imgWidth, imgHeight, extraBytes, thresholdValue);

                img.UnlockBits(bmd);
                bmpR.UnlockBits(bmdR);

                return bmpR;
            }
        }

        public static byte[,] ReflectFast(byte[,] mask)
        {
            int width = mask.GetLength(1);
            int height = mask.GetLength(0);

            byte[,] newMask = new byte[height, width];
            PHFastCpp.PHMskReflection(mask, newMask, width, height);
            return newMask;
        }
    }
}
